import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import { WeaLocaleProvider, WeaRightMenu, WeaDialog } from 'ecCom';
import * as dialogTypes from '../../../constants/OdocCompsDialogType';
import SignInput from '../public/SignInput';
import { Button } from 'antd';
import '../public/signInputLog.less';
import { toJS } from 'mobx';


const { getLabel } = WeaLocaleProvider;
/**
 * 为自己追加意见
 */
@inject('appendSelfSignLogStore', 'odocCompsStore')
@observer
class AppendSelf extends Component {
  constructor(props) {
    super(props);
    this.state = {
      height: 320,
    };
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.visible != nextProps.visible && nextProps.visible) {
      this.loadData();
    }
  }

  componentWillMount() {
    this.loadData();
  }

  loadData = () => {
    const { appendSelfSignLogStore, odocCompsStore = {} } = this.props;
    const { doInit } = appendSelfSignLogStore;
    let params = {},
      visible = false;
    try {
      let { requestParam, dialogVisible } = odocCompsStore;
      visible = dialogVisible.get(dialogTypes.ODOC_APPEND_SIGNLOG_SELF);
      params = toJS(requestParam);
      delete params.markInfo;
    } catch (e) {
      params.requestid = '11014012';// 开发热部署测试
      visible = true;
    }
    if (visible) {
      doInit(params, this.onCancel);
    }
  }

  render() {
    const { appendSelfSignLogStore, odocCompsStore = {} } = this.props;
    let visible = false;
    try {
      const { dialogVisible } = odocCompsStore;
      visible = dialogVisible.get(dialogTypes.ODOC_APPEND_SIGNLOG_SELF);
    } catch (e) {
      visible = true;// 开发热部署测试
    }

    const { height } = this.state;
    const { coreparams = {} } = appendSelfSignLogStore;
    const { title = getLabel(519035, '追加意见') } = coreparams;
    let dialogparams = {
      style: { width: 800, height },
      visible,
      title,
      icon: 'icon-coms-Need-feedback',
      onCancel: this.onCancel,
      buttons: this.getButtons(),
      layout: jQuery('.wf-req')[0],
    };
    return (
      <div>
        <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@w6bcmn`} {...dialogparams} className={'signInputDialog'}>
          <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@ccixho`} datas={this.getRightMenu()}>
            <div className={'content'}>
              {this.getSignInputContent()}
            </div>
          </WeaRightMenu>
        </WeaDialog>
      </div>
    );
  }

	/**
	 * 签字意见
	 * @returns {*}
	 */
  getSignInputContent = () => {
    const { odocCompsStore = {} } = this.props;
    const { appendSelfSignLogStore } = this.props;
    let { refid, sumitParams = {}, signinput, showSignInput = false } = appendSelfSignLogStore;
    const { signInputRequired } = sumitParams;
    const { workflowStores = {} } = odocCompsStore;
    signinput = toJS(signinput);

    return (<div className={'signInput'}>
      {showSignInput && <SignInput ecId={`${this && this.props && this.props.ecId || ''}_SignInput@3t80z1`}
        coreparams={{
          title: getLabel(31259, '追加'),
          refid,
          signInputRequired,
          premarkinfo: signinput, // TODO 注意这里仅选择一个数据
        }}
        workflowStores={workflowStores}
        allFunction={{ changecallback: this.changeSignInput }}
      />}
    </div>);
  }

  changeSignInput = (v) => {
    const { appendSelfSignLogStore } = this.props;
    const { changeSubmitParams } = appendSelfSignLogStore;
    changeSubmitParams(v);
  }

	/**
	 * 取消
	 */
  onCancel = (res = {}) => {
    const { appendSelfSignLogStore, onClose } = this.props;
    const { destory } = appendSelfSignLogStore;
    typeof onClose == 'function' && (onClose(res));
    typeof destory == 'function' && (destory());
  }

	/**
	 * 获取右键菜单
	 * @returns {Array}
	 */
  getRightMenu() {
    let btns = [];
    const { appendSelfSignLogStore } = this.props;
    const { loading = false } = appendSelfSignLogStore;
    const showAllBtns = this.allBtns();
    showAllBtns && showAllBtns.map((item) => {
      if (item.key != 'close') {
        if (!loading) {
          item.disabled = 'true';
        }
        btns.push({ ...item });
      } else {
        btns.push({ ...item });
      }
    });
    return btns;
  }

	/**
	 * dialog获取底部按钮
	 * @returns {Array}
	 */
  getButtons = () => {
    let buttons = [];
    const { appendSelfSignLogStore } = this.props;
    const { loading = false } = appendSelfSignLogStore;
    const showAllBtns = this.allBtns();
    showAllBtns && showAllBtns.map((item) => {
      if (item.key != 'close') {
        if (!loading) {
          item.disabled = 'true';
        }
        buttons.push([<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@4jr9cf@${item.key}`} {...item}>{item.content}</Button>]);
      } else {
        buttons.push([<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9rz0mz@${item.key}`} {...item}>{item.content}</Button>]);
      }
      // buttons.push([<Button {...item}>{item.content}</Button>])
    });
    return buttons;
  }

	/**
	 * 统一管理btns
	 * @returns {{key: string, icon: *, content: *, onClick: Index.goSave, type: string}[]}
	 */
  allBtns = () => {
    const { appendSelfSignLogStore, pluginbuttons = [], odocCompsStore = {} } = this.props;
    const { workflowStores = {} } = odocCompsStore;

    const { doSave } = appendSelfSignLogStore;
    let showAllBtns = [
      {
        key: 'save',
        icon: <i className="icon-coms-Preservation" />,
        content: getLabel(519035, '追加意见'),
        onClick: () => doSave(workflowStores, this.onCancel),
        type: 'primary',
      },
      {
        key: 'close',
        icon: <i className="icon-coms-go-back" />,
        content: getLabel(201, '取消'),
        onClick: this.onCancel,
        type: 'ghost',
      },
    ];
    pluginbuttons && pluginbuttons.length >= 1 && (showAllBtns = showAllBtns.concat(pluginbuttons));
    return showAllBtns;
  }
}

export default AppendSelf;
